package com.spc.boot.common.security;

import com.spc.boot.common.security.entity.DbUser;
import com.spc.boot.common.security.mapper.SecurityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;


@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Resource
    SecurityMapper securityMapper;

    @Override
    public UserDetails loadUserByUsername(String loginId) throws UsernameNotFoundException {

        DbUser dbUser = securityMapper.selectUserByLoginId(loginId);

        if (null == dbUser) {
            throw new UsernameNotFoundException(loginId);
        }

        User user = new User(loginId, dbUser.getPassword(), true, true, true, true, new ArrayList<>());

        return user;

    }
}
